package tree;

import offer.TreeNode;

public class leetCode110_isBalanced {
    public boolean isBalanced(TreeNode root) {
        if(root == null) return true;
        return build(root) >= 0;

    }

    public int build(TreeNode root){
        if(root == null) return 0;

        int left = build(root.left);
        if(left == -1){
            return -1;
        }
        int right = build(root.right);
        if(right == -1){
            return -1;
        }
        if(Math.abs(left - right) > 1){
            return -1;
        }else{
            return Math.max(left,right) + 1;
        }

    }


}
